//
//  FXStatusOriginalView.swift
//  微博(Swift)
//
//  Created by V on 16/1/2.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit
import SnapKit


// cell 里面的间距
let FXStatusCellMargin: CGFloat = 8
// 头像的宽高
let FXStatusCellIconViewWH: CGFloat = 35

class FXStatusOriginalView: UIView {
    
    // 记录当前控件的底部约束
    var bottomConstraint: Constraint?
    
    // 当前视图引用的视图模型
    var statusViewModel: FXStatusViewModel?{
        didSet{
            // 名字
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 头像
            if let urlString = statusViewModel?.status?.user?.profile_image_url {
            iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar_default_small"))
            }
            // 设置微博内容
            contentLabel.text = statusViewModel?.status?.text
            // TODO: 时间与来源，先这么显示，然后后面会处理
            timeLabel.text = statusViewModel?.status?.created_at
            sourceLabel.text = statusViewModel?.status?.source
            // 会员
            memberView.image = statusViewModel?.memberImage
            // 认证
            verifiedView.image = statusViewModel?.verifiedImage
            
            // 判断原创微博是否有图片
            bottomConstraint?.uninstall()
            if let picurls = statusViewModel?.status?.pic_urls where picurls.count > 0 {
                // 有配图：1.显示配图视图 2.更新约束 3.设置要显示的数据
                statusPictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(statusPictureView).offset(FXStatusCellMargin).constraint
                })
                statusPictureView.picUrls = picurls
            }else{
                // 没有配图：1.隐藏视图配图 2.更新约束
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(FXStatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    // 时间与来源的label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 10)
    // 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    // 认证的图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 内容的label
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        // 多行属性
        label.numberOfLines = 0
        return label
    }()
    // 原创微博图片视图
    private lazy var statusPictureView: FXStatusPictureView = {
        let statusPictureView = FXStatusPictureView()
        // statusPictureView.backgroundColor = RandomColor()
        return statusPictureView
    }()

    
    

    // 设置视图内容
    private func setupUI(){
//        backgroundColor = RandomColor()
        
        nameLabel.text = "郭大大"
        timeLabel.text = "时间"
        sourceLabel.text = "原创微博"
        
        // 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        // 图片视图
        addSubview(statusPictureView)
        
        // 添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(FXStatusCellMargin)
            make.top.equalTo(self).offset(FXStatusCellMargin)
            make.size.equalTo(CGSizeMake(FXStatusCellIconViewWH, FXStatusCellIconViewWH))
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-3)
            make.centerY.equalTo(iconView.snp_bottom).offset(-3)
        }
        
        // 名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(FXStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(FXStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(FXStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        // 内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(FXStatusCellMargin)
            // 约束宽度
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -FXStatusCellMargin)
        }
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(statusPictureView).offset(FXStatusCellMargin).constraint
        }
        
        // 配图视图
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(FXStatusCellMargin)
            make.leading.equalTo(contentLabel)
        }

    }
    
}
